﻿<?php
include 'config.php';
include 'opendb.php';
//////////////
error_reporting(0);
function runSQL($rsql) {
	mysql_query("set names 'utf8'");  //处理中文信息
	$result = mysql_query($rsql) or die ('数据库连接出错'); 
	return $result;
	mysql_close($connect);
}

function countRec($fname,$tname,$where) {
$sql = "SELECT count($fname) FROM $tname $where";
$result = runSQL($sql);
if ($row = mysql_fetch_array($result)) {return $row[0];}//只有一条记录，用if而不用while
}

$page = $_POST['page'];
$rp = $_POST['rp'];
//分类信息
$Latname=$_POST['Latname'];      $Genus=$_POST['Genus'];        $Family=$_POST['Family'];
//馆藏信息
$Barcode=$_POST['Barcode'];      $TypeMode=$_POST['TypeMode'];  $Photos=$_POST['Photos'];
//采集信息
$collector=$_POST['Collector'];  $colNum=$_POST['ColNum'];      $ColYear=$_POST['ColYear'];
//空间信息
$Geotag=$_POST['Geotag'];         $smallALT=$_POST['smallALT'];  $LargeALT=$_POST['LargeALT'];
//$Country=$_POST['Country'];
$sql="";

    if(strlen($Latname)>2)   $sql.=" and ScientificName='".$Latname."'";
    if(strlen($Genus)>2)     $sql.=" and Genus='".$Genus."'";  
    if(strlen($Family)>2)    $sql.=" and Family='".$Family."'"; 
    if(strlen($Barcode)>2)   $sql.=" and CatalogNumber='".$Barcode."'";
 
    if($Photos=="1")         $sql.=" and length(ImageURL)>4"; 

   if(strlen($colNum)>2)    $sql.=" and CollectorNumber='".$colNum."'";
  
  if(strlen($ColYear)>2)   $sql.=" and left(DateCollected,4)='".$ColYear."'";
   
   if(strlen($collector)>2) $sql.=" and Collector like '%".$collector."%'"; 
   
  // if(strlen($Country)>1)   
   if(strlen($Geotag)>1)   $sql.=" and (Geotags like '%".$Geotag."%')";	  //  跨字段检索

  if($smallALT!='')        $sql.=" and MinimumElevation>".$smallALT." and MaximumElevation<".$LargeALT;
 
 //$sql.=" and Country='".$Country."'";
  mysql_query("set names 'utf8'");
$cleanSql=preg_replace("/and/","",$sql,1);  //只替换开始的第一个and，其余的则保留

////////////////////////////////

if (!$page) $page = 1;
if (!$rp) $rp = 10;

$start = (($page-1) * $rp);

$limit = "limit $start, $rp";

$sql = "SELECT * FROM ".$main_table." where ".$cleanSql." $limit";

mysql_query("set names 'utf8'");  //处理中文信息
$result = runSQL($sql);
$total = countRec('GlobalUniqueIdentifier',$main_table,' where '.$cleanSql);//' where '.$where);

header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "page: $page,\n";
$json .= "title: $page,\n";
$json .= "total: $total,\n";
$json .= "rows: [";
$rc = false;

while ($row = mysql_fetch_array($result)) {
 //对学名的LSID的进行判别，如果有的话就直接附上LSID链接///
  if(strlen($row['LSID'])>2) $sname='<a href="lsid.php?lsid=urn:lsid:cvh.org.cn:names:'.$row['LSID'].
   '" target="_blank" title="链接至该物种的LSID页面">'.$row['ScientificName'].'</a>';
   else $sname='<a href="'.$lsid_reg.'" style="color:red" target="_blank" title="无对应的LSID页面，链接至主馆登记页面">'.$row['ScientificName'].'</a>';
   
 $sp_lsid='<a href="specimen.php?lsid=urn:lsid:cvh.org.cn:specimen:'.
           $row['GlobalUniqueIdentifier'].'" target="_blank">'.$row['CatalogNumber'].'</a>';
		   
    if(strlen($row['ImageURL'])>2) $hasPic='<img src="././pics/img.jpg" />';
    else $hasPic='';
   /////////////////		   
 //////////////////////
 if ($rc) $json .= ",";
$json .= "\n{";
$json .= "id:'".$row['GlobalUniqueIdentifier']."',";

$json .= "cell:['".$sp_lsid."'"; 
$json .= ",'".addslashes($row['Family'])."'";
$json .= ",'".addslashes($sname)."'";
$json .= ",'".addslashes($row['CommonName'])."'";
$json .= ",'".addslashes($Country)."'";//$hasPic

$json .= ",'".addslashes($row['Collector'])."'";
$json .= ",'".addslashes($row['CollectorNumber'])."'";
$json .= ",'".addslashes($row['DateCollected'])."'";

$json .= ",'".addslashes($row['StateProvince'])."'";
$json .= ",'".addslashes($row['County'])."'";
$json .= ",'".addslashes($row['County'])."']";
$json .= "}";
$rc = true;
}
$json .= "]\n";
$json .= "}";
echo $json;
?>